#!/bin/bash

# 设置导出目录
EXPORT_DIR="./export"
mkdir -p $EXPORT_DIR

# 获取所有镜像名称并去重
IMAGES=$(docker compose config | grep image: | awk '{print $2}' | sort | uniq)

# 遍历每个镜像并导出
for IMAGE in $IMAGES
do
    # 导出镜像为 tar 文件
    TAR_FILE=$EXPORT_DIR/$(echo $IMAGE | tr '/' '_').tar
    docker save -o $TAR_FILE $IMAGE
    echo "Exported $IMAGE to $TAR_FILE"

    # 压缩 tar 文件为 tar.gz
    gzip $TAR_FILE
    echo "Compressed $TAR_FILE to $TAR_FILE.gz"
    echo "-----------"
done

echo "All images have been exported and compressed to $EXPORT_DIR"
